package com.ecity.table {
	import com.ecity.events.GameState;
	import com.ecity.util.UIFactory;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author yicheng.zhang
	 */
	public class HitButton extends TableDisplayObject{
		private static const CLASS_KEY:String = "button";
		private var disable:Boolean = false;
		public function hitTest(rx:int, ry:int, key:String):Boolean {
			var layer:Sprite= this.getLayer(key);
			if (layer.getChildAt(0) == null) {
				this.removeChild(layer);
				return false;
			}
			if (!disable) {
				var bmp:Bitmap = layer.getChildAt(0) as Bitmap;
				var rect:Rectangle = new Rectangle(rx*20-bmp.x,ry*20-bmp.y,20,20);
				var isHit:Boolean = bmp.bitmapData.hitTest(new Point(), 0xff, rect);
				if (isHit) {
					disable = true; 
					GameState.hitable = false;
					this.removeLayer(key);
				}
				return isHit;
			}else {
				return false;
			}
		}
		
		public function addButton(key:String):void {
			var cfgXml:XML = UIFactory.getConfiger();
			var layer:Sprite= this.getLayer(key);
			var btn:Bitmap = UIFactory.getBitmapbyName(key) as Bitmap; 
			btn.x= cfgXml.layout[CLASS_KEY].btn.(@id==key).@x;
			btn.y= cfgXml.layout[CLASS_KEY].btn.(@id == key).@y;
			disable = false;
			trace("keykeykeykey:",key);
			layer.addChild(btn);
		}
		
	}

}